//
//  HPSignInViewController.m
//  HappyPenguin
//
//  Created by zhuangyihang on 1/11/16.
//  Copyright © 2016 zhuangyihang. All rights reserved.
//

#import "HPSignInViewController.h"
#import "Director.h"
#import "HPSignInTextField.h"
#import "HPSignUpViewController.h"
#import "HPSignInRequest.h"

static const NSInteger kViewMarginLeft = 15;
static const NSInteger kViewMarginTop = 100;

static const NSInteger kPasswordTextFieldMarginTop = 15;

static const NSInteger kSavePwdButtonMarginTop = 20;

static const NSInteger kLoginButtonMarginTop = 20;

@interface HPSignInViewController ()


@property (nonatomic, strong) HPSignInTextField *userTextField;
@property (nonatomic, strong) HPSignInTextField *pwdTextField;
@property (nonatomic, assign) BOOL saveCheck;

@end

@implementation HPSignInViewController

- (id)init{
    self = [super init];
    if (self) {
        self.saveCheck = YES;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setBaseNavigationBarHidden:YES];
    
    [self setupUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - Private
- (void)setupUI{
    WS(ws);
    
    UIImageView *v = [UIImageView new];
    [self.baseContentView addSubview:v];
    [v mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(self.baseContentView);
        make.center.equalTo(self.baseContentView);
    }];
    v.image = [UIImage imageNamed:@"SignIn_Background"];
    v.contentMode = UIViewContentModeCenter;
    
    HPSignInTextField *v1 = [HPSignInTextField new];
    [self.baseContentView addSubview:v1];
    
    [v1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.baseContentView).offset(-kViewMarginLeft*2);
        make.centerX.equalTo(self.baseContentView);
        make.height.equalTo(@40);
        make.top.equalTo(self.baseContentView).offset(kViewMarginTop);
    }];
    
    v1.textField.placeholder = @"输入手机号码";
    v1.textField.textColor = [UIColor whiteColor];
    v1.iconImageView.image = [UIImage imageNamed:@"SignIn_UserIcon"];
    self.userTextField = v1;
    
    HPSignInTextField *v2 = [HPSignInTextField new];
    [self.baseContentView addSubview:v2];
    [v2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.centerX.equalTo(v1);
        make.top.equalTo(v1.mas_bottom).offset(kPasswordTextFieldMarginTop);
    }];
    v2.textField.placeholder = @"输入密码";
    v2.textField.textColor = [UIColor whiteColor];
    v2.iconImageView.image = [UIImage imageNamed:@"SignIn_UserPassword"];
    self.pwdTextField = v2;
    
    UIButton *savePwdButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.baseContentView addSubview:savePwdButton];
    [savePwdButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(v2.mas_bottom).offset(kSavePwdButtonMarginTop);
        make.left.equalTo(v1);
    }];
    [savePwdButton setTitle:@"记住我" forState:UIControlStateNormal];
    savePwdButton.titleLabel.font = [UIConstants getFont:13];
    [self updateSaveButtonStatus:savePwdButton];
    [savePwdButton bk_addEventHandler:^(id sender) {
        ws.saveCheck = !ws.saveCheck;
        [ws updateSaveButtonStatus:sender];
    } forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *forgetPwdButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [forgetPwdButton setTitle:@"忘记密码?" forState:UIControlStateNormal];
    [self.baseContentView addSubview:forgetPwdButton];
    [forgetPwdButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(v2);
        make.bottom.equalTo(savePwdButton);
    }];
    forgetPwdButton.titleLabel.font = [UIConstants getFont:13];
    NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:@"忘记密码?"];
    NSRange titleRange = {0,[title length]};
    [title addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:titleRange];
    [forgetPwdButton setAttributedTitle:title
                      forState:UIControlStateNormal];
    
    
    UIButton *signUp = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.baseContentView addSubview:signUp];
    signUp.adjustsImageWhenHighlighted = NO;
    [signUp setImage:[UIImage imageNamed:@"SignIn_SignUpButton"] forState:UIControlStateNormal];
    [signUp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(forgetPwdButton.mas_bottom).offset(kLoginButtonMarginTop);
        make.right.equalTo(v1);
    }];
    [signUp bk_addEventHandler:^(id sender) {
        HPSignUpViewController *v = [[HPSignUpViewController alloc] init];
        [ws.navigationController pushViewController:v animated:YES];
    } forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *signIn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.baseContentView addSubview:signIn];
    signIn.adjustsImageWhenHighlighted = NO;
    [signIn setImage:[UIImage imageNamed:@"SignIn_LoginButton"] forState:UIControlStateNormal];
    [signIn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.baseContentView);
        make.right.equalTo(signUp.mas_left).offset(5);
        make.top.equalTo(signUp);
    }];
    [signIn bk_addEventHandler:^(id sender) {
        [ws signIn];
    } forControlEvents:UIControlEventTouchUpInside];
    
    [v1.textField becomeFirstResponder];
    
#ifdef DEBUG
    
    v1.textField.text = @"888";
    v2.textField.text = @"123123";
#endif
}

- (void)updateSaveButtonStatus:(UIButton *)button{
    if(self.saveCheck){
        [button setImage:[UIImage imageNamed:@"SignIn_SaveCheck"] forState:UIControlStateNormal];
    }else{
        [button setImage:[UIImage imageNamed:@"SignIn_SaveUnCheck"] forState:UIControlStateNormal];
    }
}

#pragma mark - Click
- (void)signIn{
    WS(ws);
    
    [[HUD hud] showInfoHUD:@"正在登录"];
    
    HPSignInRequest *request = [[HPSignInRequest alloc] init];
    request.responseBlock = ^(HPSignInResponse *response, HttpError *error){
        if (error) {
        }else{
            [[HUD hud] showSuccessHUD:@"登录成功"];
            [[Director sharedInstance] setLoginUser:response.user save:YES];
            [NSObject runBlockAfterDelay:1.0 block:^{
                [ws.navigationController pushViewController:[[Director sharedInstance]getTabViewController] animated:YES];
            }];
            
        }
    };
    request.usercode = self.userTextField.textField.text;
    request.password = self.pwdTextField.textField.text;
    
    if (request.usercode.length==0) {
        
    }else if(request.password.length<6){
        
    }
    
    [request send];

}


@end
